import Vue from 'vue'
import Router from 'vue-router'

const home=()=>import("@/views/home")
const explore=()=>import("@/views/userCenter/explore")
const userCenter=()=>import("@/views/userCenter")
const userCenterHome=()=>import('@/views/userCenter/userCenterHome')
const myGallery=()=>import("@/views/userCenter/myGallery")
const personalCenter=()=>import("@/views/userCenter/personalCenter")
const userManage=()=>import("@/views/admin/user")
const oAuth=()=>import("@/components/oauth")
const githubLogin=()=>import("@/components/githubLogin")
const shortUrl=()=>import("@/views/userCenter/shortUrl")
const uploadImg=()=>import("@/views/upload")

Vue.use(Router)

const routes=[
  { 
    path: '/',
    name: 'home',
    component: home,
    meta:{
      title:'SUK图床  - 简单实用的图床程序',
      isPublic:true
    }
  },
  {
    path: '/userCenter',
    name: 'userCenter',
    component: userCenter,
    children:[
      {
        path: '/',
        name: 'userCenterHome',
        component:userCenterHome,   
        meta:{
          title:'用户中心  - SUK图床'
        }
      },
      {
        path: 'myGallery',
        name: 'myGallery',
        component:myGallery,   
        meta:{
          title:'我的图库  - SUK图床'
        }  
      },
      {
        path: 'personalCenter',
        name: 'personalCenter',
        component:personalCenter,
        meta:{
          title:'个人中心 - SUK图床'
        }      
      },
      {
        path: 'shortUrl',
        name: 'shortUrl',
        component:shortUrl,
        meta:{
          title:'短网址列表 - SUK图床'
        }      
      },
      {
        path: '/explore',
        name: 'explore',
        component:explore,
        meta:{
          title:'探索图片 - SUK图床',
          isPublic:true
        }         
      },
      {
        path: '/userManage',
        name: 'userManage',
        component:userManage,
        meta:{
          title:'用户管理 - SUK图床',
          isAdmin:true
        }       
      },
      {
        path: '/imageManage',
        name: 'imageManage',
        component:myGallery,   
        meta:{
          title:'图片管理 - SUK图床',
          isAdmin:true
        }    
      }
    ]
  },
  {
    path:'/oAuth',
    name:'oAuth',
    component:oAuth
  },
  {
    path:'/githubLogin',
    name:'githubLogin',
    component:githubLogin,
    meta:{
      isPublic:true
    }
  },
  {
    path:'/uploadImg',
    name:'uploadImg',
    component:uploadImg
  },
  {
    path:'*',
    redirect:'/'
  }
]

const router = new Router({
  mode:'history',
  routes
})


router.beforeEach ((to, from, next) => {
  let token=sessionStorage.getItem('token');
  let userId=sessionStorage.getItem('id');
  if(to.meta.title) document.title = to.meta.title;
  if(!to.meta.isPublic){
    if(!token){
      alert('还没登录呢，请先登录~~~~~'); 
      next('/login')
    }
    else{
      if(userId!=='ht5dX14zczAyV1rnb2MzOT4Y' && to.meta.isAdmin){
        alert('你不是管理员，没有权限访问该地址~~~');
        next('/');
      }
      else next();
    }
  }
  else next();
});

export default router

